<?php
	//控制器基类，所有action继承此类
	class controller
	{
		var $error = array( 'php' => array(), 'sql' => array() );
		var $before = array( 'get' => array(), 'post' => array() );
		var $after = array( 'get' => array(), 'post' => array() );
		var $input = array();
		var $debug = true;
		var $method = 'get';

		function controller()
		{
			session_start();

			if( isset($_SERVER['HTTP_X_FORWARDED_FOR']) )
			{
				if( strpos( $_SERVER['HTTP_X_FORWARDED_FOR'], ',' ) > 0 )
				{
					$server_vars = explode( ',', $_SERVER['HTTP_X_FORWARDED_FOR'] );
					$_SERVER['REMOTE_ADDR'] = $server_vars[0];
				}
				else
				{
					$_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_X_FORWARDED_FOR'];
				}
			}

			//包含配置文件
			$_dirname = pathinfo( dirname( __FILE__ ) );
			require_once $_dirname['dirname'] . '/inc/config.php';

			//出现php错误时的处理方法
			set_error_handler( array( & $this, 'mark' ) );
			//set_magic_quotes_runtime( 0 );
			ini_set( 'magic_quotes_gpc', 0 );
			ini_set( 'magic_quotes_runtime', 0 );
			date_default_timezone_set( 'Asia/Shanghai' );

			//ob_start();
			header( 'Content-type: text/html; charset=utf-8' );
			if ( ! ( $this->debug || isset( $this->input['_debug_'] ) ) ) error_reporting( 0 );
		}

		//处理get请求
		function do_get()
		{
			echo __FUNCTION__;
		}

		//处理post请求
		function do_post()
		{
			echo __FUNCTION__;
		}

		//处理http请求
		function run()
		{
			$this->input = $_SERVER['REQUEST_METHOD'] == 'GET' ? $this->clean( $_GET ) : $this->clean( $_POST );

			$method = strtolower( $_SERVER['REQUEST_METHOD'] );
			$this->method = $method;
			$handle = true;

			foreach( $this->before[$method] as $function => $contine )
			{
				if ( ! $this->$function() )
				{
					$handle = $contine;
					break;
				}
			}

			if ( $handle ) $this->{'do_'.$method}();

			foreach( $this->after[$method] as $function => $contine ) if ( ! $this->$function() ) break;
			if ( ! $this->right() && ( ( $this->debug || isset( $this->input['_debug_'] ) ) ) ) $this->alert();
		}

		//显示提示信息
		function prompt( $data, $url, $time = 3 )
		{
			$template = load( 'template', array( 'domain'=>'tpl.admin' ) );
			$template->assign( 'data', $data );
			$template->assign( 'time', $time );
			$template->assign( 'url', $url );
			$template->assign( 'default', 0 );
			echo $template->parse( 'prompt.php' );
		}

		//出错时的提示方法
		function alert()
		{
			ob_clean();
			echo '<pre>';
			print_r( $this->error );
			echo '</pre>';
		}

		//判断php语法和sql执行是否正常
		function right()
		{
			$database = load( 'database' );
			foreach( $database->sql as $index => $value ) foreach( $value as $sql => $error ) if ( $error != '' ) $this->error['sql'][$sql] = $error;
			return count( $this->error['php'] ) == 0 && count( $this->error['sql'] ) == 0;
		}

		//格式化传入参数
		function format( $value )
		{
			if ( get_magic_quotes_gpc() )
			{
				$value = strip_tags( ( trim( $value ) ) );
			}
			else
			{
				$value = addslashes( strip_tags( ( trim( $value ) ) ) );
			}
			return   $value;
		}

		//统一处理传入的参数
		function clean( & $array )
		{
			foreach( $array as $key	=> $value ) $array[$key] = is_array( $array[$key] ) ? $this->clean( $value ) : $this->format( $value );
			return $array;
		}

		//出错时的处理方法
		function mark( $no, $str, $file, $line )
		{
			if ( $no != 2048 ) $this->error['php'][] = array( 'no' => $no, 'file' => $file, 'line' => $line, 'str' => $str );
		}

	}
?>